using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MMM.Core.Domain;
using ServiceStack.ServiceHost;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using MMM.Services.Base;
using MMM.Services.Operations;
using MMM.Services.Type;
using MMM.Core;
using MMM.Services.Response;

namespace MMM.Services
{
    /// <summary>
    /// 类描述 基础服务类(POST,PUT)
    /// </summary>
    public class ProductGroupService : BaseService<ProductGroup>
    {
    }
    /// <summary>
    /// 类描述 服务类(GET)
    /// </summary>
    public class ProductGroupGetSerice : BaseGetService<ProductGroupPageGet, ProductGroup>
    {
        //protected override System.Linq.Expressions.Expression<Func<ProductGroup, bool>> CreateFilter(ProductGroupPageGet request)
        //{
        //    return base.CreateFilter(request);
        //}
        public override object OnGet(ProductGroupPageGet request)
        {
            IRepository<BagManageInfo> bmRep = ApplicationContext.Container.GetInstance<IRepository<BagManageInfo>>();
            List<ProductGroup> pdList = new List<ProductGroup>();
            if (!string.IsNullOrEmpty(request.ContractCode))
            {

                var bmList = bmRep.GetAll(t => t.ContractCode == request.ContractCode);

                var pdList1 = Rep.GetAll(t => t.ContractCode == request.ContractCode);

                if (bmList != null)
                {
                    foreach (var item in bmList)
                    {
                        ProductGroup productGroup = pdList1.FirstOrDefault(t => t.ContractCode == item.ContractCode && t.ProductID == item.ProductID);
                        ProductGroup model = new ProductGroup();
                        if (productGroup != null)
                        {
                            productGroup.Price = item.Price;
                            model = productGroup;
                        }
                        else
                        {
                            model.Id = Guid.NewGuid();
                            model.IsAdd = true;
                            model.Price = item.Price;
                            model.Product = item.Product;
                            model.ProductID = item.ProductID;
                            model.ProductType = item.ProductType;
                            model.AreaCode = item.AreaCode;
                            model.AreaName = item.AreaName;
                            model.CompanyID = item.CompanyID;
                            model.ContractCode = item.ContractCode;
                        }
                        pdList.Add(model);
                    }
                }
                if (pdList1 != null)
                {
                    foreach (var item in pdList1)
                    {
                        if (!pdList.Contains(item))
                        {
                            pdList.Add(item);
                        }
                    }
                }
            }
            //List<ActQuantity> presult = lst.OrderBy(t => t.Number).Skip(request.Limit * (request.Page - 1)).Take(request.Limit).ToList();
            var resultList = pdList.OrderBy(t => t.ContractCode).Skip(request.Limit * (request.Page - 1)).Take(request.Limit).ToList();
            return new OperationResult<ProductGroup>(resultList, pdList.Count());
        }
    }
    /// <summary>
    /// 类描述 服务类(DELETE)
    /// </summary>
    public class ProductGroupDeleteService : BaseDeleteService<ProductGroup, ProductGroupDelete>
    {
    }
}
